﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DMDichVu : DBInteractionBase
    {
        public string MaDichVu { get; set; }
        public string TenDichVu { get; set; }
        public string TriSoBinhThuongNam { get; set; }
        public string TriSoBinhThuongNu { get; set; }
        public double DonGiaDV { get; set; }
        public double DonGiaBH { get; set; }
        public string MaNhomDichVu { get; set; }

        public bool Insert(DMDichVu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMDichVu_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaDichVu", ConvertNvarcharToVarchar(obj.MaDichVu)));
                    dbcmd.Parameters.Add(new SqlParameter("@TenDichVu", obj.TenDichVu));
                    dbcmd.Parameters.Add(new SqlParameter("@TriSoBinhThuongNam", obj.TriSoBinhThuongNam));
                    dbcmd.Parameters.Add(new SqlParameter("@TriSoBinhThuongNu", obj.TriSoBinhThuongNu));
                    dbcmd.Parameters.Add(new SqlParameter("@DonGiaDV", obj.DonGiaDV));
                    dbcmd.Parameters.Add(new SqlParameter("@DonGiaBH", obj.DonGiaBH));
                    dbcmd.Parameters.Add(new SqlParameter("@MaNhomDichVu", obj.MaNhomDichVu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DMDichVu obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMDichVu_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaDichVu", obj.MaDichVu));
                    dbcmd.Parameters.Add(new SqlParameter("@TenDichVu", obj.TenDichVu));
                    dbcmd.Parameters.Add(new SqlParameter("@TriSoBinhThuongNam", obj.TriSoBinhThuongNam));
                    dbcmd.Parameters.Add(new SqlParameter("@TriSoBinhThuongNu", obj.TriSoBinhThuongNu));
                    dbcmd.Parameters.Add(new SqlParameter("@DonGiaDV", obj.DonGiaDV));
                    dbcmd.Parameters.Add(new SqlParameter("@DonGiaBH", obj.DonGiaBH));
                    dbcmd.Parameters.Add(new SqlParameter("@MaNhomDichVu", obj.MaNhomDichVu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Delete(string id)
        {
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMDichVu_Delete", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaDichVu", id));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectById(string id)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMDichVu_SelectById", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaDichVu", id));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMDichVu_SelectById: " + ex.Message);
            }
        }

        public DataTable SelectAll(DMDichVu obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("tb_DMDichVu_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaDichVu", string.IsNullOrEmpty(obj.MaDichVu) ? "" : obj.MaDichVu));
                    dbcmd.Parameters.Add(new SqlParameter("@TenDichVu", string.IsNullOrEmpty(obj.TenDichVu) ? "" : obj.TenDichVu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaNhomDichVu", string.IsNullOrEmpty(obj.MaNhomDichVu) ? "" : obj.MaNhomDichVu));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("tb_DMDichVu_SelectAll: " + ex.Message);
            }
        }
    }
}
